package com.sh.dc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sh.dc.common.BaseResult;
import com.sh.dc.common.enums.BizCodeEnum;
import com.sh.dc.common.pojo.Contants;
import com.sh.dc.common.pojo.bo.PutObjectRes;
import com.sh.dc.common.pojo.bo.SignFileReq;
import com.sh.dc.common.utils.FileUtil;
import com.sh.dc.entity.FileResource;
import com.sh.dc.service.IFileResourceService;
import com.sh.dc.service.IOSSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private IOSSService ossService;

    @Autowired
    private IFileResourceService fileResourceService;

    @PostMapping("/sign")
    public BaseResult<String> signUrl(@RequestBody SignFileReq req) {
        String objectName = req.getObjectName();
        if (objectName.startsWith(Contants.OSS_PREFIX)) {
            objectName = objectName.substring(Contants.OSS_PREFIX.length());
        }
        return BaseResult.success(ossService.signUrl(objectName, req.getExpire()));
    }

    /**
     * 声学上传文件接口
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/acoustics/upload")
    public BaseResult uploadForAcoustics(@RequestParam("file") MultipartFile file) throws IOException {
        String hashCode = FileUtil.calFileFingerPrint(file.getInputStream(), FileUtil.ALGORITHM_MD5);
        QueryWrapper<FileResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hash_code", hashCode);
        FileResource fileResource = fileResourceService.getOne(queryWrapper);
        if (fileResource != null) {
            return BaseResult.success(PutObjectRes.builder()
                    .key(fileResource.getFileUrl())
                    .fileResourceId(fileResource.getId())
                    .build());
        }
        String filename = file.getOriginalFilename();
        String newFileName = filename.substring(0, filename.lastIndexOf(".")) + "_"  +
                hashCode + filename.substring(filename.lastIndexOf("."));
        String key = FileUtil.generateFileOssKey(newFileName);
        PutObjectRes uploadRet = ossService.putObject(key, file);
        if (uploadRet.getSuccess()) {
            fileResource = new FileResource();
            fileResource.setFileName(file.getOriginalFilename());
            fileResource.setFileSize(file.getSize());
            fileResource.setFileUrl(key);
            fileResource.setContentType(file.getContentType());
            fileResource.setHashCode(hashCode);
            fileResource.setEtag(uploadRet.getEtag());
            fileResourceService.save(fileResource);

            uploadRet.setFileResourceId(fileResource.getId());
            uploadRet.setKey(key);
            return BaseResult.success(uploadRet);
        } else {
            return BaseResult.fail(BizCodeEnum.FILE_UPLOAD_ERROR);
        }
    }

}
